package thread;

public class SyncDemo4 {
    public static void main(String[] args) {
        Boo boo = new Boo();
        Thread t1 = new Thread(){
            public void run(){
                boo.methodA();
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                boo.methodB();
            }
        };
        t1.start();
        t2.start();
    }
}
class Boo {
    public synchronized void methodA() {
        Thread t = Thread.currentThread();
        System.out.println(t.getName() + ":程序A开始");
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
        }
        System.out.println(t.getName() + ":程序A结束");
    }

    public void methodB() {
        synchronized (this) {
            Thread t = Thread.currentThread();
            System.out.println(t.getName() + ":程序B开始");
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
            }
            System.out.println(t.getName() + ":程序B结束");
        }
    }
}